import { createSelector } from 'reselect'
import { LogTypes, LogStatus } from '../common/enums'
import { getIfStepResultValid } from '../common/getIfStepResultValid'

// ROOT
export const getRootStateSelector = state => state
export const getNotifySelector = state => state.notify
export const getActiveTabSelector = state => state.activeTab
export const getActiveTabStateSelector = createSelector([getRootStateSelector, getActiveTabSelector], (state, activeTab) => state[activeTab.replace(/\s/g, '')])
export const getActivePageOfActiveTabSelector = createSelector([getActiveTabStateSelector], (activeTabState) => activeTabState.activePage || '')
export const getIfCollaseCol0OfActiveTabSelector = createSelector([getActiveTabStateSelector], (activeTabState) => activeTabState.collapseCol0)
export const getIfCollaseCol1OfActiveTabSelector = createSelector([getActiveTabStateSelector], (activeTabState) => activeTabState.collapseCol1)

// RUNNER
export const getRunnerSelector = state => state.runner
export const getRunnerActiveSuitIdSelector = createSelector([getRunnerSelector], runner => runner.acitveSuiteId)

// SUITE
export const getSuiteActiveSuiteSelector = state => state.suite
export const getSuiteActiveSuiteIdSelector = createSelector([getSuiteActiveSuiteSelector], suite => suite.acitveSuiteId)
export const getSuiteActiveSuiteTitlleSelector = createSelector([getSuiteActiveSuiteSelector], suite => suite.title)
export const getSuiteActiveSuiteLevelSelector = createSelector([getSuiteActiveSuiteSelector], suite => suite.level)
export const getSuiteActiveSuiteModeSelector = createSelector([getSuiteActiveSuiteSelector], suite => suite.mode)
export const getSuiteForceReloadIndexSelector = createSelector([getSuiteActiveSuiteSelector], suite => suite.forceReloadIndex)

// STEP COLLECTION
export const getStepCollectionActiveStepCollectionSelector = state => state.stepCollection
export const getStepCollectionActiveStepCollectionIdSelector = createSelector([getStepCollectionActiveStepCollectionSelector], stepCollection => stepCollection.acitveStepCollectionId)
export const getStepCollectionForceReloadIndexSelector = createSelector([getStepCollectionActiveStepCollectionSelector], stepCollection => stepCollection.forceReloadIndex)
export const getStepCollectionActiveStepCollectionTitlleSelector = createSelector([getStepCollectionActiveStepCollectionSelector], stepCollection => stepCollection.title)
export const getStepCollectionActiveStepCollectionModeSelector = createSelector([getStepCollectionActiveStepCollectionSelector], stepCollection => stepCollection.mode)

// COMMON
export const getCommonSelector = state => state.common
export const getCommonAllPlansSelector = createSelector([getCommonSelector], common => common.allPlans || [])
export const getCommonAllPagesSelector = createSelector([getCommonSelector], common => common.allPages || [])
export const getCommonAllEnvsSelector = createSelector([getCommonSelector], common => common.allEnvs || [])
export const getCommonAllCookiesSelector = createSelector([getCommonSelector], common => common.allCookies || [])
export const getCommonForceReloadAllPlansIndexSelector = createSelector([getCommonSelector], common => common.forceReloadAllPlansIndex)

// PROGRESS
export const getProgressSelector = state => state.progress
export const getProgressActiveSuiteSelector = createSelector([getProgressSelector], progress => progress.activeSuite)
export const getProgressForceReloadLogsSelector = createSelector([getProgressSelector], progress => progress.forceReloadActivePlanLogsIndex)
export const getProgressActiveSuiteIdSelector = createSelector([getProgressActiveSuiteSelector], activeSuite => activeSuite.id)
export const getProgressActivePlanSelector = createSelector([getProgressSelector], progress => progress.activePlan)
export const getProgressActivePlanIdSelector = createSelector([getProgressActivePlanSelector], activePlan => activePlan ? activePlan.id : '')
export const getProgressActivePlanLogsSelector = createSelector([getProgressSelector], progress => progress.activePlanLogStat)
export const getProgressActiveSuiteLogsSelector = createSelector([getProgressSelector], progress => progress.activeSuiteLogs)
export const getProgressActiveSuiteStepListSelector = createSelector([getProgressSelector], progress => progress.activeSuiteStepList)
export const getProgressActivePlanSuitesSelector = createSelector([getProgressSelector], progress => progress.activePlanSuites)
export const getProgressActivePlanLogStatSelector = createSelector([getProgressActivePlanSuitesSelector, getProgressActivePlanLogsSelector], (activePlanSuites, logStat) => {
    const defaultStat = { pass: 0, failure: 0, total: 0 }

    let result = activePlanSuites.reduce((prev, suite) => {
        const suiteId = suite.id

        return {
            ...prev,
            [suiteId]: {
                // step总数
                total: (logStat[suiteId] || defaultStat).total,
                // step执行结果pass，且result为valid（如果是非assert直接true，如果assert则看具体断言类型而定）
                pass: (logStat[suiteId] || defaultStat).pass, // logStat.filter(log => log.suiteId === suiteId && log.type === LogTypes.step && (log.status === LogStatus.pass) && getIfStepResultValid(log.step.code, log.result)).length,
                // step执行结果failrue，或者执行结果pass且result的invalid，主要是断言失败
                failure: (logStat[suiteId] || defaultStat).failure //logStat.filter(log => log.suiteId === suiteId && log.type === LogTypes.step && (log.status === LogStatus.failure || (log.status === LogStatus.pass && !getIfStepResultValid(log.step.code, log.result)))).length
            }
        }
    }, {})

    return result
})
export const getProgressActivePlanActiveSuiteJsErrorLogsSelector = createSelector([getProgressActiveSuiteLogsSelector], logs => logs.filter(log => log.type === LogTypes.js_error))

// ELEMENT
export const getElementSelector = state => state.element
export const getElementActivePageTreeSelector = createSelector([getActivePageOfActiveTabSelector, getCommonAllPagesSelector], (pageName, allPages) => {
    const page = allPages.find(page => page.name === pageName)
    return page ? { name: page.title, toggled: true, children: page.tree || [] } : { name: '[空]', toggled: true, children: [] }
})
export const getElementActiveNodeSelector = createSelector([getElementSelector], element => element.activeNode)

// AJAX
export const getAjaxSelector = state => state.ajax

// MOCK
export const getMockSelector = state => state.mock
export const getMockActiveAjaxIdSelector = createSelector([getMockSelector], mock => mock.acitveAjaxId)

// CONFIG
export const getConfigSelector = state => state.config
export const getActiveConfigTypeSelector = createSelector([getConfigSelector], config => config.acitveConfigType)

// LOG
export const getLogSelector = state => state.log
export const getLogStatSelector = createSelector([getLogSelector], log => log.stat)

// PLAN
export const getPlanSelector = state => state.plan
export const getPlanActivePlanIdSelector = createSelector([getPlanSelector], plan => plan.activePlanId)
export const getPlanAllAddedPlansSelector = createSelector([getPlanSelector], plan => plan.addedPlans)
export const getPlanAllPlansSelector = createSelector([getPlanAllAddedPlansSelector, getCommonAllPlansSelector], (addedPlans, allPlans) => addedPlans.concat(allPlans))